home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.ridgecrest.ca.us!owens!mcclung
- From: mcclung@owens.ridgecrest.ca.us (Scott McClung)
- Subject: Alignment restrictions...
- X-Nntp-Posting-Host: owens
- Message-ID: <DKxHxG.EK1@ridgecrest.ca.us>
- Sender: usenet@ridgecrest.ca.us (Ridgenet Usenet admin)
- Organization: Science Applications International Corporation
- Date: Tue, 9 Jan 1996 19:24:52 GMT
-
- Hi,
-
- In architectures that have alignment restrictions on certain types (i.e.
- ints aligned on 4 byte boundaries, etc.), does anyone have any clean
- way of retrieving values that are not aligned correctly?
-
- I was porting some code out that had statements like:
-
- short value; /* 16 bit value */
- char *ptr; /* 32 bit pointer, no alignment restrictions */
-
- value = *(short *)ptr;
-
- It worked on, say, Intel x86, or on values of ptr that happened to be
- aligned (auto variables, in this case), but is not a general solution
- on SPARC or other RISC architectures (MIPS and PA-RISC have similar
- restrictions, if I recall.) The pointer in question happened to be
- into an mmap()'ed image file, so the alignment is not guaranteed. Bus
- errors.
-
- I replaced it with:
-
- value = *ptr << 8 | *(ptr + 1);
-
- Which works. I'd just like to find out how this is generally dealt
- with in code... Actually, a magic compiler switch would be nice. :-)
- (I'm working with gcc 2.7.2, BTW.) I'll probably replace the
- statement above with some inline function that checks the alignment of
- ptr, and acts appropriately. I guess I'm just looking for your
- collective wisdom on this topic.
-
- Any suggestion of a better group to post this in will also be helpful.
-
- Thanks...
-
- --
- /* Scott McClung Opinions expressed here are mine.
- * UNIX Software Engineer/UNIX System Admin
- * mcclung@imt.saic.com
- * mcclung@ridgecrest.ca.us
- */
-